package com.unionx.unionxuf.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unionx.unionxuf.entity.Role;
import com.unionx.unionxuf.entity.UaAccount;
import com.unionx.unionxuf.entity.User;
import com.unionx.unionxuf.service.LoginService;

@Controller

@RequestMapping(value="/login")
public class LoginController {
	

	HttpSession session =null;
	
	
	@Autowired(required=true)
	private LoginService loginService;
	
	@RequestMapping(value="/get/corp")
	@ResponseBody
	public Object getAllCorpInfo(String  rows,String page){
	      	int pageSize =Integer.parseInt(rows);
	      	int pageNumber = Integer.parseInt(page);
	      
	      	return	loginService.getAllCorpInfo(pageSize,pageNumber);
	
	
	 }

	@RequestMapping(value="/get/corps")
	@ResponseBody
	public Object getAllCorps(){
	
	
			return	loginService.getAllCorps();
	
	
	 }
	
/*	@RequestMapping(value="/get/user")
	@ResponseBody
	public Object getUserInfo(String username,String password){
		
		 return loginService.getUserInfo(username,password);
		
	}*/
	
	@RequestMapping(value="/update/user")
	@ResponseBody
	public Object updateUserPassword(String userid,String password){
	 
		return loginService.updateUserPassword(userid, password);
		
	}
	
	@RequestMapping(value="/get/user")
	public Object getUserInfo(HttpServletRequest request){
		
		
		session = request.getSession();
        session.isNew();
		//创建流水号
		

		String nowTime="";
		Date dt=new Date();
		DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		nowTime= df.format(dt);

		
		String str[] = nowTime.split(" ");
	
		
		String str1[] = str[0].split("/");
		String year = str1[0];
		String month =str1[1];
		String day = str1[2];
		
		String str2[] =str[1].split(":");
		String hour = str2[0];
		String min = str2[1];
		String sec = str2[2];
		
		long t1=System.currentTimeMillis();  
		 String  tt1 = t1+"";
		 String dtd = tt1.substring(tt1.length()-2, tt1.length());
		
		String code = year+month+day+hour+min+sec+dtd;
		String lscode = code;
	
		
		session.setAttribute("lscode", lscode);
		
	
	String cpid = request.getParameter("cccorp");
	String corpid=null;
	if(cpid==null||cpid==""){
		
	}else{
		corpid = cpid.replaceFirst("^0*", "");  
		session.setAttribute("cropid", corpid);
		session.setAttribute("cpid", cpid);
	}
	
	
	String ccusername = request.getParameter("ccusername");
	String ccuserpwd =request.getParameter("ccuserpwd");
	String logindate = request.getParameter("logindate");
	 
	
	User  u = loginService.getUser(ccusername, ccuserpwd);
	if(u.getId()!=null){
		session.setAttribute("name", ccusername);	
		session.setAttribute("pwd",ccuserpwd );
		session.setAttribute("logindate", logindate);
	
		return "/BillLayout";
	}
	
	return "/home2";
	
		
	}
	@RequestMapping(value="/close")
	@ResponseBody
	public Object Exit(){
		
		session=null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", 1);
		return map;
	}

}
